#!/bin/bash
#
# Pre build hook for Docker Automated Build.
#
# From the DOCKER_TAG environment variable, the Dockerfile, baseimage and image
# version to use are determined.  Using this information, the real Dockerfile
# is generated, replacing the dummy one.
#
# NOTE: This hook expects to run on builds triggered by git tags.
#

set -e # Exit immediately if a command exits with a non-zero status.
set -u # Treat unset variables as an error.

# Load mandatory environment variables.
source hooks/load_vars

# Load Docker build arguments.
if [ ! -f versions/"$BASEIMAGE_DOCKER_TAG" ]; then
    echo "ERROR: Invalid BASEIMAGE_DOCKER_TAG"
    exit 1
fi
source versions/"$BASEIMAGE_DOCKER_TAG"

echo "$(basename "$0") hook environment variables:"
( set -o posix ; set | sort | sed 's/^/    /')

# TODO: Validation disabled until the the following issue is resolved:
#       https://github.com/RedCoolBeans/dockerlint/issues/47
#echo "Validating Dockerfile..."
#docker run --rm -v "$(pwd)/$DOCKERFILE:/Dockerfile:ro" redcoolbeans/dockerlint

echo "$(basename "$0") hook terminated successfully."
